package net.guerlab.mall.seller.print.websocket;

import org.apache.commons.lang3.StringUtils;

import net.guerlab.mall.seller.print.entity.offline.GoodsInfo;
import net.guerlab.mall.seller.print.entity.offline.OrderInfo;
import net.guerlab.mall.seller.print.printer.PrintHelper;

public class OfflinePrintBusiness implements BusinessInterface {

    private static final String OFFSET = "&";
    private static final String GOODS_OFFSET = "#";
    private static final String GOODS_INFO_OFFSET = "\\|";

    public void onMessage(String message) {
        if (StringUtils.isBlank(message)) {
            return;
        }
        
        String[] orderinfos = message.split(OFFSET);
        
        if (orderinfos == null || orderinfos.length != 4) {
            return;
        }
        
        OrderInfo orderInfo = new OrderInfo();
        
        orderInfo.setOrderSn(orderinfos[0]);
        orderInfo.setOrderAmount(orderinfos[1]);
        orderInfo.setStoreName(orderinfos[2]);
        
        String[] goodsInfoStrings = orderinfos[3].split(GOODS_OFFSET);
        
        for (String goodsInfoString : goodsInfoStrings) {
            if (StringUtils.isBlank(goodsInfoString)) {
                continue;
            }
            
            String[] goodsInfos = goodsInfoString.split(GOODS_INFO_OFFSET);
            
            if (goodsInfos == null || goodsInfos.length != 5) {
                continue;
            }
            
            GoodsInfo goodsInfo = new GoodsInfo();
            
            goodsInfo.setSkuNum(goodsInfos[0]);
            goodsInfo.setSkuName(goodsInfos[1]);
            goodsInfo.setPrice(goodsInfos[2]);
            goodsInfo.setQuantity(goodsInfos[3]);
            goodsInfo.setTotlePrice(goodsInfos[4]);
            
            orderInfo.addGoodsInfos(goodsInfo);
        }
        
        if (orderInfo.getGoodsInfos().isEmpty()) {
            return;
        }
        
        PrintHelper.printOffline(orderInfo);
    }

}
